package com.kexinde.wjifen.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kexinde.wjifen.model.Shopkeeper;
import com.kexinde.wjifen.model.Worker;
import com.kexinde.wjifen.service.IWorkerManager;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 员工信息管理
 * 
 * @author vincent
 * 
 */
@Component("Worker")
@Scope("prototype")
public class WorkerAction extends ActionSupport implements RequestAware,
		SessionAware {
	/**
	 * 员工帐号管理
	 * 
	 * @return
	 */
	private Worker worker;
	private Shopkeeper shopkeeper;
	private IWorkerManager workerManager;
	private Map<String, Object> request;
	private Map<String, Object> session;

	public String showAllWorkers() { // 分页显示所有员工帐号
		shopkeeper = (Shopkeeper) session.get("shopkeeper");
		if (shopkeeper != null) {
			List<Worker> workers = this.workerManager.showAllWorkers(shopkeeper
					.getUserName());
			this.request.put("workers", workers);
			return "showworker";
		} else
			return ERROR;
	}

	public String showSpecificWorker() { // 显示指定员工帐号所有信息
		worker = (Worker) session.get("worker");
		worker = this.workerManager.showWorker(worker.getUserName());
		request.put("worker", worker);
		return SUCCESS;
	}

	public String add() { // 添加员工帐号
		if (this.workerManager.add(worker))
			return SUCCESS;
		else
			return ERROR;
	}

	public String modify() { // 修改员工帐号
		if (this.workerManager.modify(worker))
			return SUCCESS;
		else
			return ERROR;
	}

	public String delete() { // 删除员工帐号
		if (this.workerManager.delete(worker))
			return SUCCESS;
		else
			return ERROR;
	}

	public String login() {
		if (this.workerManager
				.check(worker.getUserName(), worker.getPassword())) {
			session.put("worker", worker);
			return SUCCESS;
		} else
			return ERROR;
	}

	public String logout() {
		session.remove("worker");
		return SUCCESS;
	}

	public String exist() {
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> seesion) {
		// TODO Auto-generated method stub
		this.session = seesion;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public Worker getWorker() {
		return worker;
	}

	public void setWorker(Worker worker) {
		this.worker = worker;
	}

	public Shopkeeper getShopkeeper() {
		return shopkeeper;
	}

	public void setShopkeeper(Shopkeeper shopkeeper) {
		this.shopkeeper = shopkeeper;
	}

	public IWorkerManager getWorkerManager() {
		return workerManager;
	}

	@Resource
	public void setWorkerManager(IWorkerManager workerManager) {
		this.workerManager = workerManager;
	}
}
